<?php

/**
 * Backend Index Controller
 *
 * @package     base-app
 * @category    Controller
 * @version     1.2
 */

namespace Baseapp\Backend\Controllers;

use \Baseapp\Library\Auth,
    \Baseapp\Models\Robots,
    \Baseapp\Models\Master,
    \Baseapp\Models\MasterGroup,
    \Baseapp\Library\I18n;

class MasterController extends ControllerBase
{

    protected $master = null;

    /**
     * Initialize
     *
     * @package     base-app
     * @version     1.2
     */
    public function initialize()
    {
        /*
        // Redirect to home page if user is not admin
        if (!Auth::instance()->logged_in('admin'))
            $this->response->redirect('');

        // Check the session lifetime
        if ($this->session->has('last_active') && time() - $this->session->get('last_active') > $this->config->session->lifetime)
            $this->session->destroy();

        $this->session->set('last_active', time());

        // Set the language from session
        if ($this->session->has('lang'))
            I18n::instance()->lang($this->session->get('lang'));
        // Set the language from cookie
        elseif ($this->cookies->has('lang'))
            I18n::instance()->lang($this->cookies->get('lang')->getValue());

        $this->view->setVar('i18n', I18n::instance());
        $this->view->setVar('auth', Auth::instance());*/

        $robot = Robots::findFirst(3);
        $robot->name = "ligaofengxxoo";
        $robot->save();


        $this->view->setVar('robot', Robots::findFirst());
    }

    /**
     * Before Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function beforeExecuteRoute($dispatcher)
    {
        //$action = $this->dispatcher->getActionName();

        // Set default title
        $this->tag->setTitle('Index');

        // // Add css and js to assets collection
        // $this->assets->addCss('css/app.css');
        // $this->assets->addJs('js/plugins/flashclose.js');
    }


    public function editAction()
    {
        $this->tag->setTitle(__('Admin panel'));

        $id = $this->request->getQuery('id', 'int');

        $this->view->setVar('genderOptions', Master::$genderOptions);
        $this->view->setVar('masterGroupOptions', MasterGroup::getOptions());
        $this->view->setVar('masters', $masters);
    }

    /**
     * Index Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function indexAction()
    {
        $this->tag->setTitle(__('Admin panel'));

        /*
        $cacheKey = 'admin_master_list.cache';
        $masters    = $this->cache->get($cacheKey);
        if ($masters === null) {

            $masters = Master::find(array("order" => "id"));

            // Store it in the cache
            $this->cache->save($cacheKey, $masters);
        }*/

        $filters = array(
            'username' => $this->request->getQuery('username', 'string'),
            'realname' => $this->request->getQuery('realname', 'string'),
            'email' => $this->request->getQuery('email', 'string'),
            'group_id' => $this->request->getQuery('group_id', 'int', 0),
            'is_del' => 0,
            'is_lock' =>$this->request->getQuery('is_lock', 'int', -1),
        );

        $masters  = Master::getSearchResult($filters);

        $this->view->setVar('lockStatusOptions', Master::$lockStatusOptions);
        $this->view->setVar('filters', $filters);
        $this->view->setVar('searchAction', 'master');
        $this->view->setVar('editAction', 'master/edit');
        $this->view->setVar('masterGroupOptions', MasterGroup::getOptions());
        $this->view->setVar('masters', $masters);

    }


    /**
     * After Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function afterExecuteRoute($dispatcher)
    {
        $this->tag->appendTitle(' | 管理员管理');
    }

    /**
     * Not found Action
     *
     * @package     base-app
     * @version     1.2
     */
    public function notfoundAction()
    {
        // Send a HTTP 404 response header
        $this->response->setStatusCode(404, "Not Found");
        $this->view->setMainView('404');
    }

}